package com.eascs.ticket.ticket.controller;

import java.text.Format;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.sql.ObjectSqlBuilder;
import com.eascs.common.assertion.util.AssertWarnUtils;
import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.ticket.common.controller.BaseMasterViewController;
import com.eascs.ticket.common.service.TicketServiceInterface;
import com.eascs.ticket.configuration.entity.TicketTypeConstant;
import com.eascs.ticket.configuration.service.TicketTypeService;
import com.eascs.ticket.configuration.service.TicketTypeStatusService;
import com.eascs.ticket.project.service.ProjectModuleService;
import com.eascs.ticket.project.service.ProjectTicketTypeService;
import com.eascs.ticket.project.service.ProjectVersionService;
import com.eascs.ticket.ticket.entity.Ticket;
import com.eascs.ticket.ticket.form.AddTicketForm;
import com.eascs.ticket.ticket.form.CreateWithForm;
import com.eascs.ticket.ticket.form.ListTicketsForm;
import com.eascs.ticket.ticket.format.DueDateViewFormat;
import com.eascs.ticket.ticket.service.TicketAssociationService;
import com.eascs.ticket.ticket.service.TicketAttachmentService;
import com.eascs.ticket.ticket.service.TicketService;
import com.eascs.ticket.ticket.service.TicketWatcherService;
import com.eascs.web.context.RequestContext;
import com.eascs.web.exception.RedirectException;
import com.eascs.web.url.UrlBuilder;

@Controller
public class TicketViewController extends BaseMasterViewController {
	@Reference
	TicketServiceInterface ticketServiceInterface;
	@Inject
	private ProjectModuleService projectModuleService;

	@Inject
	private ProjectTicketTypeService projectTicketTypeService;

	@Inject
	private ProjectVersionService projectVersionService;

	@Inject
	private TicketService ticketService;

	@Inject
	private TicketTypeStatusService ticketTypeStatusService;

	@Inject
	private TicketAssociationService ticketAssociationService;

	@Inject
	private TicketAttachmentService ticketAttachmentService;

	@Inject
	private TicketWatcherService ticketWatcherService;

	@Inject
	private TicketTypeService ticketTypeService;

	@RequestMapping("/ticket/createWith/createSub")
	public String createWith(CreateWithForm createWithForm) {
		Ticket ticket = this.ticketService.getTicketById(createWithForm.getTicketId());
		String url = "/ticket/add";
		url = UrlUtil.appendUrl(url, "projectId", ticket.getProjectId());
		url = UrlUtil.appendUrl(url, "ticketTypeCode", TicketTypeConstant.TICKET_TYPE_CODE_SUB_TASK);
		url = UrlUtil.appendUrl(url, "parentTicketId", ticket.getId());
		throw new RedirectException(url);
	}

	@RequestMapping("/ticket/add")
	public String addTicket(AddTicketForm addTicketForm) {
		Ticket ticket = new Ticket();
		ObjectUtil.copySimpleProperties(addTicketForm, ticket);
		ViewContext.put("ticket", ticket);
		if (StringUtil.isNotBlank(ticket.getProjectId()) && StringUtil.isNotBlank(ticket.getTicketTypeCode())) {
			ViewContext.put("moduleDataSouce", projectModuleService.getProjectModuleDataSouceByProjectId(ticket.getProjectId()));
			ViewContext.put("validProjectVersionDataSouce", projectVersionService.getValidProjectVersionByProjectId(ticket.getProjectId()));
			return "/ticket/add";
		}
		return "/ticket/selectProjectAndTicketType";
	}

	@RequestMapping("/ticket/view/{ticketId}")
	public String viewTicket(@PathVariable String ticketId) {
		Ticket ticket = this.ticketService.getTicketById(ticketId);
		AssertWarnUtils.assertNotNull(ticket, "没找到该工单信息");
		String projectId = ticket.getProjectId();
		ViewContext.put("ticket", ticket);
		ViewContext.put("ticketTypeDataSouce", projectTicketTypeService.getTicketTypeDataSouceByProjectId(projectId));
		ViewContext.put("moduleDataSouce", projectModuleService.getProjectModuleDataSouceByProjectId(projectId));
		ViewContext.put("validProjectVersionDataSouce", projectVersionService.getValidProjectVersionByProjectId(projectId));
		ViewContext.put("ticketTypeStatusList", ticketTypeStatusService.getTicketTypeStatusByTicketTypeCodeAndStatus(ticket.getTicketTypeCode(), ticket.getStatus()));
		if (TicketTypeConstant.CAN_SUB_YES.equals(ticketTypeService.getTicketTypeByCode(ticket.getTicketTypeCode()).getCanSub()) && projectTicketTypeService.hasProjectTicketType(ticket.getProjectId(), TicketTypeConstant.TICKET_TYPE_CODE_SUB_TASK)) {
			ViewContext.put("canSubButton", true);
		}
		if (StringUtil.hasText(ticket.getParentTicketId())) {
			Ticket parentTicket = this.ticketService.getTicketById(ticket.getParentTicketId());
			ViewContext.put("parentTicket", parentTicket);
		}
		if (StringUtil.hasText(ticket.getVersionTicketId())) {
			Ticket versionTicket = this.ticketService.getTicketById(ticket.getVersionTicketId());
			ViewContext.put("versionTicket", versionTicket);
		}
		ViewContext.put("isVersionTicket", TicketTypeConstant.TICKET_TYPE_CODE_VERSION.equals(ticket.getTicketTypeCode()));
		ViewContext.put("hasChildTicketList", this.ticketService.getChildTicketList(ticket.getId()).size() > 0);
		buildMoreTicketInfo(ticket);
		return "/ticket/view";
	}

	public void buildMoreTicketInfo(Ticket ticket) {
		String ticketId = ticket.getId();
		PaginationInfo ticketAssociationsPaginationInfo = new PaginationInfo(this.ticketAssociationService.getTicketAssociationsByTicketId(ticketId), new String[] { "关联工单号", "项目", "标题", "工单类型", "到期日", "版本", "经办人", "关联类型" });
		ticketAssociationsPaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/ticket/opt/deleteTicketAssociation?id=") });
		ticketAssociationsPaginationInfo.setTitleButtons(new Button[] { ButtonHelper.createOpenButton(null, "/ticket/view/{1}?fromTicketId="), null, null, null, null, ButtonHelper.createOpenButton(null, "/ticket/view/{9}?fromTicketId=") });
		ticketAssociationsPaginationInfo.setSelectDataOptions(new SelectDataOption[] { null, null, null, null, null, null, null, new SelectDataOption("allTicketAssociationType", "ticketAssociationType") });
		ticketAssociationsPaginationInfo.setFormats(new Format[] { null, null, null, null, new DueDateViewFormat() });
		ViewContext.put("ticketAssociationsPaginationInfo", ticketAssociationsPaginationInfo);
		PaginationInfo ticketAttachmentPaginationInfo = new PaginationInfo(this.ticketAttachmentService.getTicketAttachmentByTicketId(ticketId), new String[] { "附件名称", "大小", "修改人", "修改时间" });
		ticketAttachmentPaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/ticket/opt/deleteTicketAttachment?id="), ButtonHelper.createOpenButton("查看", "/ticket/viewTicketAttachment?id=") });
		ticketAttachmentPaginationInfo.setSelectDataOptions(new SelectDataOption[] { null, null, new SelectDataOption("allUser", "user") });
		ViewContext.put("ticketAttachmentPaginationInfo", ticketAttachmentPaginationInfo);
		PaginationInfo ticketWatcherPaginationInfo = new PaginationInfo(this.ticketWatcherService.getTicketWatcherByTicketId(ticketId), new String[] { "关注人", "修改人", "修改时间" });
		ticketWatcherPaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createAjaxConfirmButton("删除", "/ticket/opt/deleteTicketWatcher?id=") });
		ticketWatcherPaginationInfo.setSelectDataOptions(new SelectDataOption[] { new SelectDataOption("allUser", "user"), new SelectDataOption("allUser", "user") });
		ViewContext.put("ticketWatcherPaginationInfo", ticketWatcherPaginationInfo);

		// bug、数据库、新功能、优化、子任务、版本等可展示发布注意事项
		ViewContext.put("showTicketReleaseNoteInfo", null != ticket.getPublishProdTime() || StringUtil.isNotBlank(ticket.getVersionTicketId()));
	}

	@RequestMapping("/ticket/list")
	public String listTickets(ListTicketsForm listTicketsForm) {
		ObjectSqlBuilder objectSqlBuilder = ObjectSqlBuilder.createWithOrderSql(listTicketsForm, "select id,title,project_id,ticket_type_code,status from ticket where 1=1", "order by update_date desc");
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, listTicketsForm.getPageIndex());
		sqlPageQuery.setQuerySql(objectSqlBuilder.getSql());
		sqlPageQuery.addParams(objectSqlBuilder.getParams());

		PaginationInfo ticketsPaginationInfo = new PaginationInfo(this.ticketService.findTicketDataWithPage(sqlPageQuery), new String[] { "ID", "标题", "项目", "工单类型", "状态" });
		ticketsPaginationInfo.setMoreButtons(new Button[] { ButtonHelper.createOpenButton("查看", "/ticket/view/") });
		ticketsPaginationInfo.setFirstRenderColumnIndex(0);
		ticketsPaginationInfo.setSelectDataOptions(new SelectDataOption[] { null, null, new SelectDataOption("allProject", "project"), new SelectDataOption("ticketType"), new SelectDataOption("ticketStatus") });
		ticketsPaginationInfo.setUrl(new UrlBuilder(RequestContext.getRequestUrlWithQueryString()).removeParam("pageIndex").appendParam("pageIndex", "").buildUrl());
		ViewContext.put("ticketsPaginationInfo", ticketsPaginationInfo);

		return "/ticket/list";
	}

}
